package org.example.service;

import org.example.entity.CalculateProto;
import org.example.entity.Calculatepojo;

import java.io.DataInputStream;
import java.io.DataOutputStream;

public class CalculateService {

    public Calculatepojo calculate(Calculatepojo calculate, DataInputStream in, DataOutputStream out, Class cls){
        Double result = null;
        try {
            switch (calculate.getOperator()){
                case "sub":
                    result = calculate.getNumLeft() - calculate.getNumRight();
                    break;
                case "mul":
                    result = calculate.getNumLeft() * calculate.getNumRight();
                    break;
                case "div":
                    result = calculate.getNumLeft() / calculate.getNumRight();
                    break;
                default:
                    result = calculate.getNumLeft() + calculate.getNumRight();
                    break;
            }
            calculate.setResult(result);
            calculate.setSuccess(true);
            calculate.setMsg("calculatepojo success!");
        }catch (Exception e){
            calculate.setResult(result);
            calculate.setSuccess(false);
            calculate.setMsg("calculatepojo fail!");
        }
        return calculate;
    }

    public CalculateProto.Calculate calculate(CalculateProto.Calculate calculate){
       Double result = null;
        CalculateProto.Calculate.Builder builder = CalculateProto.Calculate.newBuilder(calculate);
        try {
            switch (calculate.getOperator()){
                case "sub":
                    result = calculate.getNumLeft() - calculate.getNumRight();
                    break;
                case "mul":
                    result = calculate.getNumLeft() * calculate.getNumRight();
                    break;
                case "div":
                    result = calculate.getNumLeft() / calculate.getNumRight();
                    break;
                default:
                    result = calculate.getNumLeft() + calculate.getNumRight();
                    break;
            }
            builder.setResult(result).setSuccess(true).setMsg("calculatepojo success!");
        }catch (Exception e){
            builder.setResult(result).setSuccess(false).setMsg("calculatepojo fail!");
        }
        CalculateProto.Calculate calculateResult = builder.build();
        return calculateResult;

    }




}
